package com.future.lock.home.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.lock.R;
import com.future.lock.common.MyApp;
import com.future.lock.common.RefreshDataUtils;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.common.http.response.DefaultHttpResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditPeopleNickActivity extends BaseActivity {

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.et_name)
    EditText etName;
    private String un_user_id;
    private String remark;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_edit_people_nick);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        setTitle("修改成员昵称");
        etName.setText(remark);
    }

    @Override
    protected void initLogic() {

    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        un_user_id = extras.getString("un_user_id", "");
        remark = extras.getString("remark", "");
    }

    @OnClick(R.id.tv_save)
    public void onViewClicked() {
        String name = etName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            toast("成员昵称不能为空");
            return;
        }
        if (TextUtils.equals(name, remark)) {
            finish();
            return;
        }
        HttpUtils.getHttpApi()
                .setLockUserRemark(MyApp.getUserToken(), un_user_id, name)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        DefaultHttpResponse httpResponse = new DefaultHttpResponse();
                        httpResponse.parse(response.body());
                        if (httpResponse.success == 1) {
                            setResult(0x321);
                            finish();
                        } else {
                            if (httpResponse.code == 112) {
                                RefreshDataUtils.logout(EditPeopleNickActivity.this);
                            }
                            toast(httpResponse.msg);
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        toast(t.getMessage());
                    }
                });
    }
}
